<template>
  <div>
    <Row class="header" type="flex" justify="start">
      <Col span="24">
        <Icon class="icon" type="md-pie"/>
        <span class="title">全市当日上报人员流动比例</span>
      </Col>
    </Row>
    <br>
    <div id="flowRatio" :style="{ width: width + 'px', height: height + 'px'}"></div>
  </div>
</template>

<script>
import echarts from 'echarts'
import { getAllInOutTodayNum } from '@/api/yi-qing-fang-kong'
// import mockData from './mockData'

export default {
  name: 'FlowRatioEchart',
  data () {
    return {
      chartData: []
    }
  },
  props: {
    width: {
      type: Number,
      default: () => {
        return 500
      }
    },
    height: {
      type: Number,
      default: () => {
        return 300
      }
    }
  },
  mounted () {
    this.chart()
  },
  methods: {
    chart () {
      getAllInOutTodayNum().then(ret => {
        if (ret.data.errcode === 0) {
          this.chartData = ret.data.data
          // 数据处理
          let arr = []
          arr.push({ name: '流入', num: this.chartData.instate })
          arr.push({ name: '流出', num: this.chartData.outstate })
          this.chartData = arr
          let option = {
            title: {
              text: ''
            },
            textStyle: {
            // color: '#bababa'
            },
            legend: {
              orient: 'vertical',
              left: 10
            },
            tooltip: {
              trigger: 'item',
              backgroundColor: 'rgba(50,50,50,0.6)',
              // formatter: '{a} <br/>{b} : {c} ({d}%)'
              formatter: function (params, ticket, callback) {
                return params.value.name + ': ' + params.value.num + '人  (' + params.percent + '%)'
              }
            },
            dataset: {
              source: []
            },
            series: [
              {
                type: 'pie',
                // radius: '55%',
                radius: ['50%', '70%'],
                center: ['30%', '50%'],
                color: ['#57B1EE', '#6DD9DB', '#B8A3DF'],
                label: {
                  normal: {
                    show: false,
                    position: 'center'
                  },
                  emphasis: {
                    show: true,
                    textStyle: {
                      fontSize: '25',
                      fontWeight: 'bold'
                    }
                  }
                },
                itemStyle: {
                // 通常情况下：
                  normal: {
                  },
                  // 鼠标悬停时：
                  emphasis: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                  },
                  encode: {
                    itemName: 'name',
                    value: 'num'
                  }
                }
              }
            ]
          }
          this.chartData.forEach(item => {
            option.dataset.source.push(item)
          })
          let myChart = echarts.init(document.getElementById('flowRatio'))
          myChart.setOption(option)
        } else {
          this.$Message.error({
            content: '数据获取失败,\n' + ret.data.errmsg
          })
        }
      })
    }
  }
}
</script>

<style scoped>
  .header {
    height: 30px;
  }

  .icon {
    font-size: 28px;
    font-weight: 600;
    margin-right: 5px;
  }

  .title {
    font-size: 16px;
    font-weight: 600;
    line-height: 30px;
  }
</style>
